﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Orders;
using Invoicing.Command.Orders;
using Utils;
namespace Invoicing.Logic.Orders
{
    public class ProductOrderLogic
    {
        public static ProductOrderModel NewOrder(ProductOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new InvoicingException("没有内容");

                var sta = order.Items.GroupBy(p => new { p.ProductID, p.MaterialID, p.ProcessID, p.Size }).Select(p => new { p.Key, Ct = p.Count() });
                var cc = sta.FirstOrDefault(p => p.Ct > 1);
                if (cc!=null)
                {
                    var product = Logic.Products.ProductLogic.GetModel(cc.Key.ProductID);
                    var material = Logic.Products.ProductMaterialLogic.GetModel(cc.Key.MaterialID);
                    var process = Logic.Products.ProductProcessLogic.GetModel(cc.Key.ProcessID);

                    throw new InvoicingException(string.Format("同一张单据中不充许有相同的产品记录,{0} {1} {2} {3}。", new object[] { product.Module, material.MaterialName, process.ProcessName, cc.Key.Size }));
                }
                 

                order.HouseID = 1;
                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.CostMoney);
                
                foreach (var it in order.Items)
                {
                    
                    if (it.Quantity <= 0)
                        throw new ApplicationException("无效的数量");
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                }
                return ProductOrderCommand.NewOrder(order);
            }
            return null;
        }
        
        public static ProductOrderCollection GetOrders(ProductOrderQuery sea)
        {
            return ProductOrderCommand.GetOrders(sea);
        }

        public static ProductOrderModel GetOrder(string orderNo)
        {
            return ProductOrderCommand.GetOrder(orderNo);
        }

        public static ProductOrderModel SetBalance(ProductOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new InvoicingException("没有内容");
                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.CostMoney);
                //order.HouseID = 1;

                var sta = order.Items.GroupBy(p => new { p.ProductID, p.MaterialID, p.ProcessID, p.Size }).Select(p => new { p.Key, Ct = p.Count() });
                var cc = sta.FirstOrDefault(p => p.Ct > 1);
                if (cc != null)
                {
                    var product = Logic.Products.ProductLogic.GetModel(cc.Key.ProductID);
                    var material = Logic.Products.ProductMaterialLogic.GetModel(cc.Key.MaterialID);
                    var process = Logic.Products.ProductProcessLogic.GetModel(cc.Key.ProcessID);

                    throw new InvoicingException(string.Format("同一张单据中不充许有相同的产品记录,{0} {1} {2} {3}。", new object[] { product.Module, material.MaterialName, process.ProcessName, cc.Key.Size }));
                }

                foreach (var it in order.Items)
                {
                    if (it.Quantity == 0)
                        throw new ApplicationException("无效的数量");
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (it.MaterialID == 0)
                        throw new ApplicationException("无效的材料");
                    if (it.ProcessID == 0)
                        throw new ApplicationException("无效的加工");
                    if (string.IsNullOrEmpty(it.Size))
                        throw new ApplicationException("无效的规格");
                }
                return ProductOrderCommand.SetBalance(order);
            }
            return null;
        }
    }
}
